package com.fjnu.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fjnu.dao.ImainInstrumentDao;
import com.fjnu.entities.MainInstrument;
import com.fjnu.service.InstrumentService;
import com.util.ExcelUtil;

@Controller
public class CreateExcelController {
	    @Autowired
		private  InstrumentService  instrumentService;
		@RequestMapping(value="/createExcel")
	    public String download(HttpServletRequest request,HttpServletResponse response) throws IOException{
	        String fileName="excel文件";
	        //填充projects数据
	        List<MainInstrument> projects=createData();
	        List<Map<String,Object>> list=createExcelRecord(projects);
	        String columnNames[]={"ID","资产名","销售人","负责人","单位名","存放地"};//列名
	        String keys[]    =     {"id","name","saler","principal","unit","reserveland"};//map中的key
	        ByteArrayOutputStream os = new ByteArrayOutputStream();
	        try {
	            ExcelUtil.createWorkBook(list,keys,columnNames).write(os);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        byte[] content = os.toByteArray();
	        InputStream is = new ByteArrayInputStream(content);
	        // 设置response参数，可以打开下载页面
	        response.reset();
	        response.setContentType("application/vnd.ms-excel;charset=utf-8");
	        response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
	        ServletOutputStream out = response.getOutputStream();
	        BufferedInputStream bis = null;
	        BufferedOutputStream bos = null;
	        try {
	            bis = new BufferedInputStream(is);
	            bos = new BufferedOutputStream(out);
	            byte[] buff = new byte[2048];
	            int bytesRead;
	            // Simple read/write loop.
	            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
	                bos.write(buff, 0, bytesRead);
	            }
	        } catch (final IOException e) {
	            throw e;
	        } finally {
	            if (bis != null)
	                bis.close();
	            if (bos != null)
	                bos.close();
	        }
	        return null;
	    }
	    private List<MainInstrument> createData() {
	        return instrumentService.findManyInstrument(null,10, 1); 
	    }
	    private List<Map<String, Object>> createExcelRecord(List<MainInstrument> projects) {
	        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
	        Map<String, Object> map = new HashMap<String, Object>();
	        map.put("sheetName", "sheet1");
	        listmap.add(map);
	        MainInstrument project=null;
	        for (int j = 0; j < projects.size(); j++) {
	            project=projects.get(j);
	            Map<String, Object> mapValue = new HashMap<String, Object>();
	            mapValue.put("id", project.getId());
	            mapValue.put("name", project.getInstrumentName());
	            mapValue.put("unit", project.getUnit().getUnitName());
	            mapValue.put("reserveland", project.getReserveland().getReserveLandName());
	            listmap.add(mapValue);
	        }
	        return listmap;
	    }

}
